package cn.edu.ctbu.labtest.service.impl;

import cn.edu.ctbu.labtest.dao.StudentRepository;
import cn.edu.ctbu.labtest.domain.Student;

import cn.edu.ctbu.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService {
    @Autowired
    StudentRepository studentRepository;


    /**
     * 读取所有的学生数据
     */
    @Override
    public List<Student> findAll(){

        return studentRepository.findAll();
    }

    @Override
    public List<Student> findByName(String name) {
        return studentRepository.findByName(name);
    }

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    @Override
    public Page<Student> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    };


    public Page<Student> findBySex(Integer sex,Pageable pageable){
        return studentRepository.findBySex(sex,pageable);
    }



    public List<Student> findByExample(Example<Student> student){


        return studentRepository.findAll(student);
    }

    /**
     * 分页，带过滤的查询
     * @param pageable
     * @return
     */

//    @Override
//    public List<Student> getAbcAll(List<Student> student){
////        return studentRepository.findAll(pageable);
//
//        return null;
//    };


    public  List<Student> getAll(List<Student> students){
        return null;
    }


    /**
     * 插入学生
     * @param student
     * @return
     */
    public Student insert(Student student){

        return studentRepository.save(student);
    }


    /**
     * 更新学生
     * @param student
     * @return
     */
    public Student update(Student student){

        return studentRepository.save(student);
    }

    public void delete(Student student){

        studentRepository.delete(student);
    }

    public void delete(Integer id){

        studentRepository.deleteById(id);
    }
    public Student getById(Integer id){
        Student student=studentRepository.findById(id).orElse(null);

        return student;
    }
}
